package com.monitor.app_monitor.config;
import com.monitor.app_monitor.config.ExpDatasourceConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import ru.yandex.clickhouse.ClickHouseConnection;
import ru.yandex.clickhouse.ClickHouseDataSource;
import ru.yandex.clickhouse.settings.ClickHouseProperties;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.SQLException;


@Slf4j
@Configuration
public class ClickHouseConfig {

    @Resource
    private ExpDatasourceConfiguration expDatasourceConfiguration;


    public  Connection getConn(String databases) {

        ClickHouseConnection conn = null;
        ClickHouseProperties properties = new ClickHouseProperties();
        properties.setUser(expDatasourceConfiguration.getUsername());
        properties.setPassword(expDatasourceConfiguration.getPassword());
        properties.setDatabase(databases);
        properties.setSocketTimeout(expDatasourceConfiguration.getSocketTimeout());
        ClickHouseDataSource clickHouseDataSource = new ClickHouseDataSource(expDatasourceConfiguration.getAddress(),properties);
        try {
            conn = clickHouseDataSource.getConnection();
            return conn;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

}
